package mage.factory.handler;

import java.util.ArrayList;
import java.util.Hashtable;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class MetaHandler extends DefaultHandler {
	protected String CARD = "card";
	protected String NAME = "name";
	protected String SET = "set";
	protected String RARITY = "rarity";
	protected String INSTANCE = "instance";
	protected String NUMBER = "number";
	
	protected String name = "";
	protected String set = new String();
	protected String rarity = new String();
	protected String number = new String();
	protected ArrayList<MetaElement> instance = new ArrayList<MetaElement>();
	protected Hashtable<String,ArrayList<MetaElement>> name_to_set = new Hashtable<String, ArrayList<MetaElement>>();
	
	@Override
	public void startElement(String qName, Attributes attributes)throws SAXException {
		
	}
	@Override
	public void endElement(String qName,String characters) throws SAXException {
			if(this.INSTANCE.equalsIgnoreCase(qName) ){
			this.instance.add(new MetaElement(this.set, this.rarity,this.number));
		}
		if(this.CARD.equalsIgnoreCase(qName) ){
			this.name_to_set.put(this.name, this.instance);
			this.instance = new ArrayList<MetaElement>();
		}
	}
	@Override
	public boolean characters(String characters) throws SAXException {
		if(this.NAME.equalsIgnoreCase(super.getCurrentElement())){this.name=characters;return true;}
		if(this.SET.equalsIgnoreCase(super.getCurrentElement())){this.set=characters;return true;}
		if(this.RARITY.equalsIgnoreCase(super.getCurrentElement())){this.rarity=characters;return true;}
		if(this.NUMBER.equalsIgnoreCase(super.getCurrentElement())){this.number=characters;return true;}
		return false;
	}
	public Hashtable<String,ArrayList<MetaElement>> getNameToSet(){
		return this.name_to_set;
	}
}

class MetaElement{
	protected String set = "";
	protected String rarity = "";
	protected String number = "";
	
	public MetaElement(String set,String rarity,String number){
		this.set= set;
		this.rarity= rarity;
		this.number= number;
	}
	public void getSet(String set){
		this.set= set;
	}
	public void getRarity(String rarity){
		this.rarity= rarity;
	}
	public String getSet(){
		return this.set;
	}
	public String getRarity(){
		return this.rarity;
	}
	public String getNumber(){
		return this.number;
	}
}